<?php

use yii\db\Migration;

/**
 * Class m171127_020727_createTableCourse
 */
class m171127_020727_createTableCourse extends Migration
{
    /**
     * @inheritdoc
     */
    public function safeUp()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB';
        }

        $this->createTable('{{%course}}', [

            'id' => $this->string(50)->notNull()->defaultValue('')->comment('课程ID'),
            'user_id' => $this->integer()->notNull()->defaultValue(0)->comment('用户ID'),
            'classify_id' => $this->integer()->notNull()->defaultValue(0)->comment('分类ID'),
            'speaker_id' => $this->integer()->notNull()->defaultValue(0)->comment('讲师ID'),

            'title' => $this->string(50)->notNull()->defaultValue('')->comment('课程名称'),
            'intro' => $this->text()->comment('课程简介'),
            'file_id' => $this->string(50)->notNull()->defaultValue('')->comment('封面ID'),
            'hour' => $this->integer()->notNull()->defaultValue(0)->comment('课时'),

            'is_credit' => $this->smallInteger()->notNull()->defaultValue(0)->comment('认证学分'),
            'is_order' => $this->smallInteger()->notNull()->defaultValue(0)->comment('顺序播放'),
            'started_at' => $this->integer()->notNull()->defaultValue(0)->comment('开课时间'),
            'ended_at' => $this->integer()->notNull()->defaultValue(0)->comment('结课时间'),

            'status' => $this->smallInteger()->notNull()->defaultValue(20)->comment('状态'),
            'created_at' => $this->integer()->notNull()->defaultValue(0)->comment('创建时间'),
            'updated_at' => $this->integer()->notNull()->defaultValue(0)->comment('更新时间'),
        ], $tableOptions);

        $this->addCommentOnTable('{{%course}}','课程信息表');

        $this->addPrimaryKey('course_id','{{%course}}','id');
    }

    /**
     * @inheritdoc
     */
    public function safeDown()
    {
        $this->dropTable('{{%course}}');
    }

    /*
    // Use up()/down() to run migration code without a transaction.
    public function up()
    {

    }

    public function down()
    {
        echo "m171127_020727_createTableCourse cannot be reverted.\n";

        return false;
    }
    */
}
